package cn.zwz.home.controller;

import cn.zwz.basics.utils.PageUtil;
import cn.zwz.basics.utils.ResultUtil;
import cn.zwz.basics.baseVo.PageVo;
import cn.zwz.basics.baseVo.Result;
import cn.zwz.data.entity.Department;
import cn.zwz.data.entity.User;
import cn.zwz.data.entity.UserRole;
import cn.zwz.data.service.IUserService;
import cn.zwz.data.utils.ZwzNullUtils;
import cn.zwz.home.entity.*;
import cn.zwz.home.service.*;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 郑为中
 */
@Slf4j
@RestController
@Api(tags = "学生管理接口")
@RequestMapping("/zwz/student")
@Transactional
public class StudentController {

    @Autowired
    private IStudentService iStudentService;

    @Autowired
    private IStudentClassService iStudentClassService;

    @Autowired
    private IStudentGradeService iStudentGradeService;

    @Autowired
    private IPerformanceService iPerformanceService;

    @Autowired
    private ITaskService iTaskService;

    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ApiOperation(value = "查询单条学生")
    public Result<Student> get(@RequestParam String id){
        return new ResultUtil<Student>().setData(iStudentService.getById(id));
    }

    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部学生个数")
    public Result<Long> getCount(){
        return new ResultUtil<Long>().setData(iStudentService.count());
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部学生")
    public Result<List<Student>> getAll(){
        return new ResultUtil<List<Student>>().setData(iStudentService.list());
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "查询学生")
    public Result<IPage<Student>> getByPage(@ModelAttribute Student student ,@ModelAttribute PageVo page){
        QueryWrapper<Student> qw = new QueryWrapper<>();
        if(!ZwzNullUtils.isNull(student.getUserName())) {
            qw.like("user_name",student.getUserName());
        }
        if(!ZwzNullUtils.isNull(student.getClassId())) {
            qw.eq("class_id",student.getClassId());
        }
        IPage<Student> data = iStudentService.page(PageUtil.initMpPage(page),qw);
        for (Student stu : data.getRecords()) {
            if(!ZwzNullUtils.isNull(stu.getClassId())) {
                StudentClass cla = iStudentClassService.getById(stu.getClassId());
                if(cla != null) {
                    stu.setClassName(cla.getTitle());
                }
            }
            // 成绩
            BigDecimal sum = BigDecimal.ZERO;
            QueryWrapper<StudentGrade> qw1 = new QueryWrapper<>();
            qw1.eq("student_id",stu.getId());
            List<StudentGrade> gradeList = iStudentGradeService.list(qw1);
            for (StudentGrade sg : gradeList) {
                sum = sum.add(sg.getValue());
            }
            // 课堂表现
            QueryWrapper<Performance> qw2 = new QueryWrapper<>();
            qw2.eq("student_id",stu.getId());
            List<Performance> performanceList = iPerformanceService.list(qw2);
            for (Performance pf : performanceList) {
                sum = sum.add(pf.getValue());
            }
            // 作业
            QueryWrapper<Task> qw3 = new QueryWrapper<>();
            qw3.eq("student_id",stu.getId());
            List<Task> taskList = iTaskService.list(qw3);
            for (Task task : taskList) {
                sum = sum.add(task.getValue());
            }
            stu.setGrade(sum);
        }
        return new ResultUtil<IPage<Student>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "增改学生")
    public Result<Student> saveOrUpdate(Student student){
        if(iStudentService.saveOrUpdate(student)){
            return new ResultUtil<Student>().setData(student);
        }
        return ResultUtil.error();
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "新增学生")
    public Result<Student> insert(Student student){
        if(ZwzNullUtils.isNull(student.getClassId())) {
            return ResultUtil.error("班级不能为空");
        }
        StudentClass cla = iStudentClassService.getById(student.getClassId());
        if(cla == null) {
            return ResultUtil.error("班级不存在");
        }
        iStudentService.saveOrUpdate(student);
        return new ResultUtil<Student>().setData(student);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "编辑学生")
    public Result<Student> update(Student student){
        if(ZwzNullUtils.isNull(student.getClassId())) {
            return ResultUtil.error("班级不能为空");
        }
        StudentClass cla = iStudentClassService.getById(student.getClassId());
        if(cla == null) {
            return ResultUtil.error("班级不存在");
        }
        iStudentService.saveOrUpdate(student);
        return new ResultUtil<Student>().setData(student);
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "删除学生")
    public Result<Object> delByIds(@RequestParam String[] ids){
        for(String id : ids){
            iStudentService.removeById(id);
        }
        return ResultUtil.success();
    }
}
